package com.itbaizhan.openhis.controller;

import com.itbaizhan.openhis.api.domain.Menu;
import com.itbaizhan.openhis.api.domain.vo.MenuTreeVo;
import com.itbaizhan.openhis.domain.dto.LoginInfoDto;
import com.itbaizhan.openhis.model.LoginUser;
import com.itbaizhan.openhis.service.LoginInfoService;
import com.itbaizhan.openhis.service.MenuService;
import com.itbaizhan.openhis.utils.SecurityUtils;
import com.itbaizhan.openhis.vo.ActiverUser;
import com.itbaizhan.openhis.vo.AjaxResult;
import com.itbaizhan.openhis.vo.DataGridView;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/system/loginInfo")
public class LoginInfoController {

    @Autowired
    private LoginInfoService loginInfoService;


    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo(){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("username",loginUser.getUsername());
        ajaxResult.put("picture",loginUser.getSysUser().getPicture());
        ajaxResult.put("roles",loginUser.getRoles());
        ajaxResult.put("permissions",loginUser.getPermissions());
        return ajaxResult;
    }

    @Autowired
    MenuService menuService;
    /**
     * 获取菜单信息
     * @return
     */
    @GetMapping("/getMenus")
    public AjaxResult getMenus(){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<Menu> menus = menuService.selectMenuTree(SecurityUtils.isAdmin(loginUser.getUserid()));
        ArrayList<MenuTreeVo> menuVos = new ArrayList<>();
        for (Menu menu : menus) {
            menuVos.add(new MenuTreeVo(menu.getMenuId().toString(),menu.getPath(),true));
        }
        return AjaxResult.success(menuVos);
    }

    @PostMapping("login/logout")
    public AjaxResult logout(){
        return AjaxResult.success("用户退出成功");
    }

    /**
     * 分页查询登录日志
     * @param loginInfoDto
     * @return
     */
    @GetMapping("listForPage")
    public AjaxResult listForPage(LoginInfoDto loginInfoDto){
        DataGridView dataGridView = loginInfoService.listForPage(loginInfoDto);
        return AjaxResult.success("查询成功",dataGridView.getData(),dataGridView.getTotal());
    }

    /**
     * 批量删除登录日志
     * @param infoIds
     * @return
     */
    @DeleteMapping("deleteLoginInfoByIds/{infoIds}")
    public AjaxResult deleteLoginInfoByIds(@PathVariable Long[] infoIds){
        return AjaxResult.toAjax(loginInfoService.deleteLoginInfoByIds(infoIds));
    }

    /**
     * 清空登录日志
     * @return
     */
    @DeleteMapping("clearLoginInfo")
    public AjaxResult clearLoginInfo(){
        return AjaxResult.toAjax(loginInfoService.clearLoginInfo());
    }
}
